package com.woniu.stafforder.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.OrderDetailDto;
import com.woniu.common.util.ResponseData;
import com.woniu.stafforder.dto.ChangeChildrenOrderStateDto;
import com.woniu.common.dto.PageDetailDto;
import com.woniu.stafforder.entity.OrderDetail;
import com.woniu.stafforder.service.OrderDetailService;
import com.woniu.stafforder.mapper.OrderDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【t_order_detail(订单详细)】的数据库操作Service实现
 * @createDate 2024-05-25 12:08:30
 */
@Service
@Slf4j
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
        implements OrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    private static final String REDIS_ORDER_DETAIL_DTO = "orderDetailDto:";
    private static final String REDIS_PAGE_DETAIL_DTO = "pageDetailDto:";

    @Override
    public OrderDetailDto queryOrderDetailById(Long childId) {
        return orderDetailMapper.queryOrderDetailById(childId);
    }

    @Override
    public Boolean updateChildrenOrderStatus(ChangeChildrenOrderStateDto changeChildrenOrderStateDto) {
        List<Long> idList = changeChildrenOrderStateDto.getIdList();
        Integer status = changeChildrenOrderStateDto.getStatus();
        idList.forEach(childId -> {
            lambdaUpdate().setSql("status = " + status)
                    .eq(OrderDetail::getId, childId)
                    .update();
        });
        return true;
    }

    /**
     * @param childId 子订单id
     * @return
     */
    @Override
    public ResponseData<?> queryPageDetail(Long childId) {
        OrderDetailDto orderDetail = queryOrderDetailById(childId);
        PageDetailDto pageDetailDto = new PageDetailDto();
        pageDetailDto.setOrderDetailDto(orderDetail);
        return new ResponseData<>().ok(pageDetailDto);
    }

    @Override
    public Long queryMainOrderNumByChildId(Long childId) {
        OrderDetailDto orderDetailDto = queryOrderDetailById(childId);
        if (ObjectUtil.isEmpty(orderDetailDto)) {
            return null;
        }
        return orderDetailDto.getOrderNum();
    }

    @Override
    public BigDecimal queryTotalPayMoneyById(List<Long> idList) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Long childId : idList) {
            OrderDetailDto orderDetailDto = queryOrderDetailById(childId);
            totalPrice = totalPrice.add(orderDetailDto.getPayMoney());
        }
        return totalPrice;
    }
}




